package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.domain.Employee;import cn.wolfcode.crm.qo.CustomerTransferQuery;
import cn.wolfcode.crm.service.ICustomerTransferService;
import cn.wolfcode.crm.service.IEmployeeService;import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;

@Controller
@RequestMapping("/customerTransfer")
public class CustomerTransferController {

    @Autowired
    private ICustomerTransferService customerTransferServiceImpl;
    @Autowired
    private IEmployeeService employeeService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerTransferQuery qo) {

        //根据角色编码 查询拥有该角色的员工 Market_Manager Market 市场经理和市场专员
        List<Employee> sellers = employeeService.selectEmployeeByRoleSn("Market_Manager", "Market");
        model.addAttribute("sellers", sellers);

        PageInfo<CustomerTransfer> pageInfo = customerTransferServiceImpl.query(qo);
        model.addAttribute("result", pageInfo);

        return "/customerTransfer/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        customerTransferServiceImpl.delete(id);
        return new JsonResult("删除成功", true);

    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(CustomerTransfer customerTransfer) {
        if (customerTransfer.getId() != null) {
            customerTransferServiceImpl.update(customerTransfer);
        } else {
            customerTransferServiceImpl.save(customerTransfer);
        }
        return new JsonResult("新增/编辑成功", true);
    }

    @RequestMapping("/gainCustomer")
    @ResponseBody
    public JsonResult gainCustomer(CustomerTransfer customerTransfer) {
        customerTransferServiceImpl.gainCustomer(customerTransfer);
        return new JsonResult("新增/编辑成功", true);
    }
}
